package com.koushikdutta.ion.loader;

import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import com.koushikdutta.async.future.Future;
import com.koushikdutta.async.future.SimpleFuture;
import com.koushikdutta.ion.Ion;
import com.koushikdutta.ion.bitmap.BitmapInfo;
import java.net.URI;

public class PackageIconLoader extends SimpleLoader {
    public Future<BitmapInfo> loadBitmap(Context context, Ion ion, String key, String uri, int resizeWidth, int resizeHeight, boolean animateGif) {
        if (uri == null || !uri.startsWith("package:")) {
            return null;
        }
        final Future<BitmapInfo> ret = new SimpleFuture();
        final String str = uri;
        final Ion ion2 = ion;
        final String str2 = key;
        Ion.getBitmapLoadExecutorService().execute(new Runnable() {
            public void run() {
                try {
                    String pkg = URI.create(str).getHost();
                    PackageManager pm = ion2.getContext().getPackageManager();
                    Bitmap bmp = ((BitmapDrawable) pm.getPackageInfo(pkg, 0).applicationInfo.loadIcon(pm)).getBitmap();
                    if (bmp == null) {
                        throw new Exception("package icon failed to load");
                    }
                    Object info = new BitmapInfo(str2, null, new Bitmap[]{bmp}, new Point(bmp.getWidth(), bmp.getHeight()));
                    info.loadedFrom = 1;
                    ret.setComplete(info);
                } catch (Exception e) {
                    ret.setComplete(e);
                }
            }
        });
        return ret;
    }
}
